// pages/login/login.js
const app = getApp()
var util =  require('../../utils/util.js');
var btnFlag = false
Page({

  /**
   * 页面的初始数据
   */
  data: {
    department: '',
    departmentRange: [],
    downImg: '/resources/to_bottom_icon.png',
    upImg: '/resources/to_up_icon.png',
    
  },

  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    if(options.status){
      let text = ''
      switch (options.status) {
        case '0':
          text = '审核中，请耐心等待...'
          break;
        case '2':
          text = '您不是本公司员工，不可进入！！！'
          break;
      }
      that.setData({
        isNot: true,
        btnDis: true,
        notTxt: text
      })
    }
    that.getDepartment()
    that.setData({
      imgSrc: that.data.downImg
    })
  },
  getDepartment:function(){
    let that = this;
    util.getAjax(app.globalData.apiUrl + '/Api/Food/get_food_content',res=>{
      console.log(res)
      if(res.data.length != 0){
        that.setData({
          departmentRange: res.data
        })
      }
    },{status: 1})
  },
  //社区选择
  departmentChange:function(e){
    console.log(e)
    this.setData({
      department: e.detail.value,
      imgSrc: this.data.downImg
    })
  },

  departmentCancel:function(){
    this.setData({
      imgSrc: this.data.downImg
    })
  },

  //选择箭头
  imgChange:function(e)
  {
    var that = this
    if(e.currentTarget.dataset.type == "部门")
    {
      if(that.data.imgSrc == that.data.downImg)
      {
        that.setData({
          imgSrc: that.data.upImg
        })
      }
    }
  },

  formSubmit:function(e){
    // console.log(e)
    if(btnFlag){
      return
    }
    btnFlag = true
    var name = e.detail.value.name
    var department = e.detail.value.department
    if(name)
    {
      var type= 'name'
      var validation = util.validation(name,type)
      if(!validation){
        util.showModal('提示','姓名输入不正确')
        btnFlag = false
        return
      }
    }else{
      util.showModal('提示','请输入姓名')
      btnFlag = false
      return
    }
    if(department == ''){
      util.showModal('提示','请选择部门')      
      btnFlag = false
      return 
    }
    this.save(name,department)
  },

  //保存登录
  save:function(name,department){
    var that = this
    wx.request({
      url: app.globalData.apiUrl + '/Api/GetUser/get_sk',
      data: {
        nickname: app.globalData.userInfo.nickName,
        openid: app.globalData.openid,
        headimgurl: app.globalData.userInfo.avatarUrl,
        name: name,
        content_id: that.data.departmentRange[department].id,
        content: that.data.departmentRange[department].content_name
      },
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded' //默认值
      },
      success: function (res) {
        // console.log(res)
        if(res.data.code == '1'){
          util.showToast('审核提交成功，请等待','success',2000)
          app.globalData.userId = res.data.userid;
          that.setData({
            isNot: true,
            btnDis: true,
            notTxt: '审核中，请耐心等待...'
          })
        }else{
          util.showModal('提示','請求失败，请重新登录')
        }
        btnFlag = false
      },
      fail: function (res) {
        wx.showModal({
          title: '提示',
          content: '进入失败，请重试进入',
          showCancel: false,
          complete (res) {
            btnFlag = false
          }
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})